package com.gzx.spring.apis.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzx.risk.core.apis.context.ApiContext;
import com.gzx.risk.core.apis.result.RiskResult;
import com.gzx.risk.core.apis.result.tanzhi.TanzhiGlobalRisk;
import com.gzx.risk.core.apis.result.tanzhi.Tanzhi_ty_d;
import com.gzx.risk.core.apis.result.tanzhi.Tanzhi_ty_e;
import com.gzx.spring.apis.utils.TanzhiUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 探知数据
 */
@Slf4j
@Service
public class TanzhiService {
    @Value("${apis.tanzhi.apiGatway}")
    private String apiGatway;
    @Value("${apis.tanzhi.apiKey}")
    private String apiKey;
    @Value("${apis.tanzhi.appSecret}")
    private String appSecret;
    @Value("${apis.tanzhi.version}")
    private String version;
    @Value("${apis.tanzhi.globalRisk.method}")
    private String globalRiskMethod;
    @Value("${apis.tanzhi.reportVersion}")
    private String reportVersion;
    @Value("${apis.tanzhi.ty-d.method}")
    private String tyDMethod;
    @Value("${apis.tanzhi.ty-e.method}")
    private String tyEMethod;
    @Value("${apis.tanzhi.env}")
    private String env;

    private final ObjectMapper objectMapper;

    public TanzhiService(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    public String global_risk(String name, String idCard, String phone) throws Exception {
        String json = "";
        if ("debug".equals(env)) {
            json = ApiExec.loadJson("/debug/tanzhi/global_risk");
        }else {
            TanzhiUtils util = new TanzhiUtils().apiGatway(apiGatway).apiKey(apiKey).appSecret(appSecret)
                    .version(version).method(globalRiskMethod);
            json = util.global_risk(name, idCard, phone);
        }
        return json;
    }

    public String ty_e(String name, String idCard, String phone) throws Exception {
        String json = "";
        if ("debug".equals(env)) {
            json = ApiExec.loadJson("/debug/tanzhi/ty_e");
        }else {
            TanzhiUtils util = new TanzhiUtils().apiGatway(apiGatway).apiKey(apiKey).appSecret(appSecret)
                    .version(version).method(tyEMethod).reportVersion(reportVersion);
            json = util.ty_e(name, idCard, phone);
        }
        return json;
    }

    public String ty_d(String name, String idCard, String phone) throws Exception {
        String json = "";
        if ("debug".equals(env)) {
            json = ApiExec.loadJson("/debug/tanzhi/ty_d");
        }else {
            TanzhiUtils util = new TanzhiUtils().apiGatway(apiGatway).apiKey(apiKey).appSecret(appSecret)
                    .version(version).method(tyDMethod).reportVersion(reportVersion);
            json = util.ty_d(name, idCard, phone);
        }
        return json;
    }

    public RiskResult global_risk(String groupName, ApiContext ctx) throws Exception {
        String apiName = "tanzhi_global_risk";
        String json = this.global_risk(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(), ctx.getUserInfo().getPhone());
        log.debug("{}:{}", apiName, json);
        TanzhiGlobalRisk obj = objectMapper.readValue(json, TanzhiGlobalRisk.class);
        obj.setGroupName(groupName);
        obj.setDataName(apiName);
        if (obj.isSuccess()) {
            ctx.add(obj);
        }else {
            log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
        }
        return obj;
    }

    public RiskResult ty_e(String groupName, ApiContext ctx) throws Exception {
        String apiName = "tanzhi_ty_e";
        String json = this.ty_e(groupName, ctx.getUserInfo().getId(), ctx.getUserInfo().getPhone());
        log.debug("{}:{}", apiName, json);
        Tanzhi_ty_e riskResult = objectMapper.readValue(json, Tanzhi_ty_e.class);
        riskResult.setGroupName(groupName);
        riskResult.setDataName(apiName);
        if (riskResult.isSuccess()) {
            ctx.add(riskResult);
        }else  {
            log.warn("{}:{}", apiName, riskResult.getMsg());
        }
        return riskResult;
    }

    public RiskResult ty_d(String groupName, ApiContext ctx) throws Exception {
        String apiName = "tanzhi_ty_d";
        String json = this.ty_d(groupName, ctx.getUserInfo().getId(), ctx.getUserInfo().getPhone());
        log.debug("{}:{}", apiName, json);
        Tanzhi_ty_d riskResult = objectMapper.readValue(json, Tanzhi_ty_d.class);
        riskResult.setGroupName(groupName);
        riskResult.setDataName(apiName);
        if (riskResult.isSuccess()) {
            ctx.add(riskResult);
        }else  {
            log.warn("{}:{}", apiName, riskResult.getMsg());
        }
        return riskResult;
    }
}
